home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3458 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q:order of evaluation
  5. Date: 23 Jan 1996 21:02:11 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4e3icj$q6o@colossus.holonet.net>
  8. References: <4dfhlu$a33$1@mhafn.production.compuserve.com> <4e2f04$bnp@vixen.cso.uiuc.edu>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Scott J. McCaughrin (sjmccaug@prairienet.org) wrote:
  13.  
  14. : In a previous article, 100336.3326@CompuServe.COM (Holger Maier) says:
  15. [...]
  16. : >  int i=1;int j=i+(i+=1);
  17. [...]
  18. :  Hi, Holger:
  19.  
  20. :  No, it appears that the compiler is correct. 
  21.  
  22. I agree, it's correct.  Any behavior is acceptable for an undefined
  23. expression.
  24.  
  25. :  The parentheses override
  26. :  any other precedence rules, so (i+=1) is executed first to set i == 2,
  27. :  then the assignment has the effect of assigning i' + 2 (= 2 + 2) to j.
  28.  
  29. No.  I recommend the comp.lang.c FAQ, if you cannot see why your
  30. reasoning is wrong; there, the expression  a[i] = i++  is analyzed
  31. and debunked.  Note that [] has the highest precedence of any of
  32. the operators in that expression, but still, the value of the index
  33. i is undefined.   The standard explicitly says so.
  34.  
  35. (BTW, it is imprecise to say that parentheses override precedence
  36. rules.  Rather, they simply *identify* their contents as an
  37. expression, whose value is to be used as operand for whatever 
  38. operators are to its left and right.  The parentheses themselves 
  39. are not an operator in this syntax, so they do not in themselves 
  40. have or imply any precedence.)
  41.  
  42. Nowhere in the standard will you find anything about parenthesized
  43. expressions getting evaluated first.  You totally made that up!
  44. For well-defined expressions without side effects, your assumption
  45. does no harm because it gives the right answer.  The harm comes 
  46. when you try to apply your assumption to undefined expressions,
  47. or to situations like the following:
  48.  
  49.    int one() { cout<<"one"; return 1; }
  50.    int two() { cout<<"two"; return 2; }
  51.  
  52.    int foo = (one() + 1) * two();
  53.  
  54. Prints "twoone" on my machine, even though by your reasoning the
  55. one() call should have come first.  Your reasoning is wrong.
  56.  
  57. It can be a rude awakening, when the scales fall from your eyes.
  58. I remember when it happened to me.  Don't worry, the world 
  59. doesn't come to an end.
  60. --
  61. Russell Blackadar,   russell@mdli.com
  62.